package com.proj.hot.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.proj.hot.GlobalParams;
import com.proj.hot.R;
import com.proj.hot.utils.DebugUtil;
import com.proj.hot.utils.ImageLoaderConfigFactory;

import org.json.JSONObject;

import java.util.List;

/**
 * Created by zangchenggang on 17/8/15.
 */
public class MovieGalleryAdapter extends BaseAdapter {

    private final String TAG = "MovieGalleryAdapter";

    private Context context;

    private List<JSONObject> list;

    private int nowposition;

    private LayoutInflater inflater;

    // private RelativeLayout gallery_item_rl;

    public MovieGalleryAdapter(Context context, List<JSONObject> list) {
        inflater = LayoutInflater.from(context);
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        //return Integer.MAX_VALUE;
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        //return list.get((i % list.size()));
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        //return i % list.size();
        return i;
    }

    public int getNowposition() {
        return nowposition;
    }

    //    @Override
//    public View getView(int i, View view, ViewGroup viewGroup) {
//        ImageView imageView = new ImageView(context);
//        TextView textView = new TextView(context);
//        //nowposition = (i % list.size());// 真正的位置
//        //DebugUtil.i(TAG, "i=" + i + ",nowposition=" + nowposition);
//        try {
//            DebugUtil.i("zzzg","适配器里面,i="+i +"logo="+list.get(i).getString("logo"));
//            ImageLoader.getInstance().displayImage(list.get(i).getString("logo"),
//                    imageView, ImageLoaderConfigFactory.getOptionsWithNopic());
//
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
//        //这里要优化，1080、720适配
//        if(GlobalParams.screenWidth==1920) {
//            imageView.setLayoutParams(new Gallery.LayoutParams(350, 620));
//        }else{
//            imageView.setLayoutParams(new Gallery.LayoutParams(350, 620));
//        }
////        ViewGroup.LayoutParams lp;
////        lp=textView.getLayoutParams();
////        lp.width=350;
////        lp.height=150;
////        textView.setLayoutParams(lp);
////        textView.setTextSize(25);
////        textView.setText(list.get(i).getString("name"));
////        textView.setBackgroundResource(R.color.retextbg);
//
//        return imageView;
//    }
    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder;
        if (view == null) {
            holder = new ViewHolder();
            view = inflater.inflate(R.layout.view_gallery_item, null);
            holder.poster = (ImageView) view.findViewById(R.id.gallery_poster);
            holder.brief = (TextView) view.findViewById(R.id.gallery_brief);
            holder.black = (TextView) view.findViewById(R.id.gallery_black);
            holder.gallery_item_rl = (RelativeLayout) view.findViewById(R.id.gallery_item_rl);
            //holder.score = (TextView) view.findViewById(R.id.gallery_score);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        try {
            DebugUtil.i("zzzg", "适配器里面,i=" + i + "logo=" + list.get(i).getString("logo"));
            ImageLoader.getInstance().displayImage(list.get(i).getString("logo"),
                    holder.poster, ImageLoaderConfigFactory.getOptionsWithNopic());
//            holder.brief.setText(list.get(i).getString("brief"));
//            holder.score.setText(list.get(i).getString("score"));
            String secondtitle = list.get(i).getString("secondtitle");
            if (null != secondtitle && !"".equals(secondtitle)) {
                holder.brief.setText(secondtitle.trim());
            } else {
                holder.brief.setText("暂无简介");
            }
//            holder.score.setText("9.0分");

        } catch (Exception e) {
            e.printStackTrace();
            DebugUtil.i(TAG, "gallery出现异常！");
        }


        if (selectItem == i) {
            //holder.poster.startAnimation(AnimFactory.zoomOutAnimation());  //选中时，这是设置的比较大
            android.view.ViewGroup.LayoutParams lp = holder.gallery_item_rl
                    .getLayoutParams();
            if(GlobalParams.screenHeight==672){//这里是适配 1280*672 、1280*720 、1920*1080适配的分辨率
                DebugUtil.i("fbl","选中当前item,672分辨率.......");
                lp.height = 460;
                lp.width = 266;
            }
            if(GlobalParams.screenHeight==720){
                DebugUtil.i("fbl","选中当前item,720分辨率.......");
                lp.height = 452;
                lp.width = 266;
            }
            if(GlobalParams.screenHeight==800){
                DebugUtil.i("fbl","选中当前item,800分辨率.......");
            }
            if(GlobalParams.screenHeight == 1080){//0.629
                DebugUtil.i("fbl","选中当前item,800分辨率.......");
                lp.height = 680;
                lp.width = 403;
            }

            holder.gallery_item_rl.setLayoutParams(lp);
            holder.black.setVisibility(View.INVISIBLE);
        } else {
            //holder.poster.startAnimation(AnimFactory.zoomInAnimation());
            android.view.ViewGroup.LayoutParams lp = holder.gallery_item_rl
                    .getLayoutParams();
            if(GlobalParams.screenHeight==672){
                lp.height = 428;
                lp.width = 238;
            }
            if(GlobalParams.screenHeight==720){
                lp.height = 428;
                lp.width = 238;
            }
            if(GlobalParams.screenHeight==800){

            }
            if(GlobalParams.screenWidth==1920){//0.468
                lp.height = 600;
                lp.width = 330;
            }

            holder.gallery_item_rl.setLayoutParams(lp);
            holder.black.setVisibility(View.VISIBLE);
        }

        return view;
    }

    private int selectItem;

    public void setSelectItem(int selectItem) {

        if (this.selectItem != selectItem) {
            this.selectItem = selectItem;
            notifyDataSetChanged();
        }
    }

    class ViewHolder {
        RelativeLayout gallery_item_rl;
        ImageView poster;
        TextView brief;
        TextView black;
        // TextView score;
    }
}
